home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gnulib / libsrc98.zoo / scanf.c < prev    next >
C/C++ Source or Header  |  1993-05-29  |  12KB  |  559 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.    This file is part of the GNU C Library.
  3.    
  4.    The GNU C Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.    
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU C Library; see the file COPYING.LIB.  If
  16.    not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17.    Cambridge, MA 02139, USA.  */
  18.  
  19. /* 
  20.  * adapted for atariST gcc lib
  21.  *
  22.  * NOTES: interface is different from equivalent gnuC lib function
  23.  *        it was munged to match our old _scanf().
  24.  *
  25.  *      if __NO_FLOAT__ is defined then the floating point stuff
  26.  *      gets nuked (for iio*.olb) as per our old scanf.c.
  27.  *
  28.  *  It is very important to read and understand the GNU Library General 
  29.  *  Public License. It specifies rights and conditions that are different
  30.  *  from the GNU copyleft.
  31.  *
  32.  *    ++jrb bammi@cadence.com
  33.  */
  34.  
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <ctype.h>
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <compiler.h>
  43.  
  44. /* the code assumes this definition, note: traditional <ctype> def
  45.  * of tolower will break the code. Ansi def should be ok.
  46.  */
  47. #ifdef tolower
  48. #  undef tolower
  49. #  define    tolower(c)    (isupper(c) ? (c)^0x20 : (c))
  50. #endif
  51.  
  52. #ifndef __NO_FLOAT__
  53. #  define FLOATS 1
  54. #else
  55. #  define FLOATS 0
  56. #endif
  57.  
  58. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  59.  
  60. #ifdef    __GNUC__
  61. #define    HAVE_LONGLONG
  62. #define    LONGLONG    long long
  63. #else
  64. #define    LONGLONG    long
  65. #endif
  66.  
  67.  
  68. #define    inchar()    ((c = ((*get)(s))) == EOF ? EOF : (++read_in, c))
  69. #define unchar(c)    (--read_in, (*unget)(c, s))
  70. #define    conv_error()    return((c == EOF || ((*unget)(c, s))), done)
  71. #define input_error()    return( done < 1 ? EOF : done )
  72. #define    memory_error()    return((errno = ENOMEM), EOF)
  73.  
  74. __EXTERN long int strtol __PROTO((const char *nptr, char **endptr, int base));
  75. __EXTERN double strtod __PROTO((const char *s, char **endptr));
  76.  
  77. /* Read formatted input from S according to the format string
  78.    FORMAT, using the argument list in ARG.
  79.    Return the number of assignments made, or -1 for an input error.  */
  80. int _scanf(s, get, unget, format, arg)
  81. register FILE *s;
  82. int (*get) __PROTO((FILE *));
  83. int (*unget) __PROTO((int, FILE *));
  84. const char *format;
  85. va_list arg;
  86. {
  87.     register const char *f = format;
  88.     register char fc;        /* Current character of the format.  */
  89.     register size_t done = 0;    /* Assignments done.  */
  90.     register size_t read_in = 0;    /* Chars read in.  */
  91.     register int c;        /* Last char read.  */
  92.     register int do_assign;    /* Whether to do an assignment.  */
  93.     register int width;        /* Maximum field width.  */
  94.     
  95.     /* Type modifiers.  */
  96.     char is_short, is_long, is_long_double;
  97. #ifdef    HAVE_LONGLONG
  98.     /* We use the `L' modifier for `long long int'.  */
  99. #define    is_longlong    is_long_double
  100. #else
  101. #define    is_longlong    0
  102. #endif
  103. #if FLOATS
  104.     /* Status for reading F-P nums.  */
  105.     char got_dot, got_e;
  106. #endif
  107.     /* If a [...] is a [^...].  */
  108.     char not_in;
  109.     /* Base for integral numbers.  */
  110.     int base;
  111.     /* Integral holding variables.  */
  112.     long int num;
  113.     unsigned long int unum;
  114. #if FLOATS
  115.     /* Floating-point holding variable.  */
  116. # ifdef __M68881__
  117.     long double fp_num;
  118. # else
  119.     double fp_num;
  120. # endif
  121. #endif
  122.     /* Character-buffer pointer.  */
  123.     register char *str;
  124.     /* Workspace.  */
  125.     char work[256];
  126.     char *w;        /* Pointer into WORK.  */
  127.     
  128.     if ((format == NULL) || (!*format))
  129.     {
  130.     errno = EINVAL;
  131.     input_error();
  132.     }
  133.     
  134. # define decimal ('.')    /* should really come from locale stuff that we dont */
  135.     /* have as yet */
  136.     c = inchar();
  137.     
  138.     /* Run through the format string.  */
  139.     while (*f != '\0')
  140.     {
  141. #if 0 /* no mb support as yet */
  142.     if (!isascii(*f))
  143.     {
  144.         /* Non-ASCII, may be a multibyte.  */
  145.         int len = mblen(f, strlen(f));
  146.         if (len > 0)
  147.         {
  148.         while (len-- > 0)
  149.             if (c == EOF)
  150.             input_error();
  151.             else if (c == *f++)
  152.             (void) inchar();
  153.             else
  154.             conv_error();
  155.         continue;
  156.         }
  157.         }
  158. #endif      
  159.     
  160.     fc = *f++;
  161.     if (fc != '%')
  162.     {
  163.         /* Characters other than format specs must just match.  */
  164.         if (c == EOF)
  165.         input_error();
  166.         if (isspace(fc))
  167.         {
  168.         /* Whitespace characters match any amount of whitespace.  */
  169.         while (isspace (c))
  170.             (void)inchar ();
  171.         continue;
  172.         }
  173.         else if (c == fc)
  174.         (void) inchar();
  175.         else
  176.         conv_error();
  177.         continue;
  178.     }
  179.     
  180.     /* Check for the assignment-suppressant.  */
  181.     if (*f == '*')
  182.     {
  183.         do_assign = 0;
  184.         ++f;
  185.     }
  186.     else
  187.         do_assign = 1;
  188.     
  189.     /* Find the maximum field width.  */
  190.     width = 0;
  191.     while (isdigit(*f))
  192.     {
  193.         width *= 10;
  194.         width += *f++ - '0';
  195.     }
  196.     if (width == 0)
  197.         width = -1;
  198.     
  199.     /* Check for type modifiers.  */
  200.     is_short = is_long = is_long_double = 0;
  201.     while (*f == 'h' || *f == 'l' || *f == 'L')
  202.         switch (*f++)
  203.         {
  204.           case 'h':
  205.         /* int's are short int's.  */
  206.         is_short = 1;
  207.         break;
  208.           case 'l':
  209. #ifdef HAVE_LONGLONG
  210.         if (is_long)
  211.             /* A double `l' is equivalent to an `L'.  */
  212.             is_longlong = 1;
  213.         else
  214. #endif
  215.             /* int's are long int's.  */
  216.             is_long = 1;
  217.         break;
  218.           case 'L':
  219.         /* double's are long double's, and int's are long long int's.  */
  220.         is_long_double = 1;
  221.         break;
  222.         }
  223.     
  224.     /* End of the format string?  */
  225.     if (*f == '\0')
  226.         conv_error();
  227.     
  228.     /* Find the conversion specifier.  */
  229.     w = work;
  230.     fc = *f++;
  231.     if (fc != '[' && fc != 'c' && fc != 'n')
  232.         /* Eat whitespace.  */
  233.         while (isspace(c))
  234.         (void) inchar();
  235.     switch (fc)
  236.     {
  237.       case '%':    /* Must match a literal '%'.  */
  238.         if (c != fc)
  239.         conv_error();
  240.         else
  241.         c = inchar();
  242.         break;
  243.         
  244.       case 'n':    /* Answer number of assignments done.  */
  245.         if (do_assign)
  246.         *va_arg(arg, int *) = read_in - 1; /* -1 is debatable ++jrb */
  247.         break;
  248.         
  249.       case 'c':    /* Match characters.  */
  250.         if (do_assign)
  251.         {
  252.         str = va_arg(arg, char *);
  253.         if (str == NULL)
  254.             conv_error();
  255.         }
  256.         
  257.         if (c == EOF)
  258.         input_error();
  259.         
  260.         if (width == -1)
  261.         width = 1;
  262. /* mjr:    */        
  263.         if (do_assign) {
  264.         do
  265.             *str++ = c;
  266.         while (inchar() != EOF && --width > 0);
  267.         } else
  268.         while (inchar() != EOF && --width > 0)
  269.             ;
  270.  
  271.         if (do_assign)
  272.         ++done;
  273.         
  274.         if (c == EOF)
  275.         input_error();
  276.         break;
  277.         
  278.       case 's':    /* Read a string.  */
  279.         if (do_assign)
  280.         {
  281.         str = va_arg(arg, char *);
  282.         if (str == NULL)
  283.             conv_error();
  284.         }
  285.         
  286.         if (c == EOF)
  287.         input_error();
  288.         
  289.         do
  290.         {
  291.         if (isspace(c))
  292.             break;
  293.         if (do_assign)
  294.             *str++ = c;
  295.         } while ((inchar() != EOF) && ((width > 0) ? --width != 0 : 1));
  296.         
  297.         if (do_assign)
  298.         {
  299.         *str = '\0';
  300.         ++done;
  301.         }
  302.         
  303.         if (c == EOF)
  304.         input_error();
  305.         break;
  306.         
  307.       case 'x':    /* Hexadecimal integer.  */
  308.       case 'X':    /* Ditto.  */ 
  309.         base = 16;
  310.         goto number;
  311.         
  312.       case 'o':    /* Octal integer.  */
  313.         base = 8;
  314.         goto number;
  315.         
  316.       case 'u':    /* Decimal integer.  */
  317.       case 'd':    /* Ditto.  */
  318.         base = 10;
  319.         goto number;
  320.         
  321.       case 'i':    /* Generic number.  */
  322.         base = 0;
  323.         
  324.       number:;
  325.         if (c == EOF)
  326.         input_error();
  327.         
  328.         /* Check for a sign.  */
  329.         if (c == '-' || c == '+')
  330.         {
  331.         *w++ = c;
  332.         if (width > 0)
  333.             --width;
  334.         (void) inchar();
  335.         }
  336.         
  337.         /* Look for a leading indication of base.  */
  338.         if (c == '0')
  339.         {
  340.         if (width > 0)
  341.             --width;
  342.         *w++ = '0';
  343.         
  344.         (void) inchar();
  345.         
  346.         if (tolower(c) == 'x')
  347.         {
  348.             /* one char look ahead to see if its really a lead ind */
  349.             int savec = c;
  350.             int peekc = inchar();
  351.             c = savec;
  352.             (void)unchar(peekc);
  353.             if(isxdigit(peekc))
  354.                     {  
  355.             if (base == 0)
  356.                 base = 16;
  357.             if (base == 16)
  358.             {
  359.                 if